package com.ztsoft.user.service.dynamic.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztsoft.user.mapper.dynamic.ColumnDescriptionUserMapper;
import com.ztsoft.user.pojo.entity.dynamic.ColumnDescription;
import com.ztsoft.user.pojo.entity.dynamic.ColumnDescriptionUser;
import com.ztsoft.user.service.dynamic.ColumnDescriptionUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ColumnDescriptionUserServiceImpl extends ServiceImpl<ColumnDescriptionUserMapper, ColumnDescriptionUser> implements ColumnDescriptionUserService {

    /**
     * 保存列
     */
    @Override
    public void saveColumnDescription(ColumnDescriptionUser columnDescriptionUser) {
        //获取添加的列
        List<Map<String, String>> listMap = columnDescriptionUser.getCorrespondingField();

        Date date = new Date(System.currentTimeMillis());
        int maxSequence = baseMapper.getMaxSequence(columnDescriptionUser.getTableCode(), columnDescriptionUser.getUserId());
        columnDescriptionUser.setCreateTime(date);
        columnDescriptionUser.setModifyTime(date);

        if (!listMap.isEmpty()) {
            for (Map<String, String> map : listMap) {
                String selectField = map.get("COLUMN_NAME");
                String title = map.get("COLUMN_COMMENT");
                maxSequence++;
                ColumnDescriptionUser columnDescription1 = new ColumnDescriptionUser();
                BeanUtils.copyProperties(columnDescriptionUser, columnDescription1);
                //设置给前端的key
                columnDescription1.setField(selectField.replace('#', '_'));
                columnDescription1.setTitle(title);
                //将 表#列  变更为 表.列
                selectField = selectField.replace('#', '.');
                //设置sql里查询的字段
                columnDescription1.setSelectField(selectField);
                columnDescription1.setSequence(maxSequence);
                baseMapper.insert(columnDescription1);
            }
        } else {
            baseMapper.insert(columnDescriptionUser);
        }
    }

    /**
     * 获取表格列
     */
    @Override
    public List<ColumnDescription> queryColumnByTableCode(ColumnDescriptionUser descriptionUser) {

        QueryWrapper<ColumnDescriptionUser> wrapper = new QueryWrapper<>(descriptionUser)
                .orderBy(true, true, "sequence");
        List<ColumnDescriptionUser> list = baseMapper.selectList(wrapper);

        List<ColumnDescription> columnDescriptionList = new ArrayList<>();
        for (ColumnDescriptionUser columnDescriptionUser : list) {
            ColumnDescription columnDescription = new ColumnDescription();
            BeanUtils.copyProperties(columnDescriptionUser, columnDescription);
            columnDescription.setDeptId(columnDescriptionUser.getUserId());
            columnDescriptionList.add(columnDescription);
        }
        return columnDescriptionList;
    }
}